package com.yan;

import com.yan.bean.Resume;
import com.yan.dao.ResumeDao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author WangYan
 * @description:
 * @date 2021/5/28 10:33
 */
@SpringBootApplication
public class MongodbTemplateMain {
    public static void main(String[] args) throws ParseException {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(MongodbTemplateMain.class, args);
        insert(applicationContext);
        find(applicationContext);
        findListByNameAndExpectSalary(applicationContext);
    }


    private static void insert(ConfigurableApplicationContext applicationContext) throws ParseException {
        ResumeDao resumeDao = applicationContext.getBean("resumeDao", ResumeDao.class);
        Resume resume = new Resume();
        resume.setName("张三");
        resume.setCity("上海");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date parse = simpleDateFormat.parse("2002-10-01 11:13:14");
        resume.setBirthday(parse);
        resume.setExpectSalary(18000);
        /**
         * 注意：
         * 插入数据的时候 如果不指定 collection（表名） 默认实体类名首字母小写作为表名
         */
        resumeDao.insertResume(resume);
    }

    private static void find(ConfigurableApplicationContext applicationContext) {
        ResumeDao resumeDao = applicationContext.getBean("resumeDao", ResumeDao.class);
        Resume resume = resumeDao.findByName("张三");
        System.out.println("-------------" + resume);
    }

    private static void findListByNameAndExpectSalary(ConfigurableApplicationContext applicationContext) {
        ResumeDao resumeDao = applicationContext.getBean("resumeDao", ResumeDao.class);
        List<Resume> resumes = resumeDao.findListByNameAndExpectSalary("张三", 18000);
        System.out.println("-------------" + resumes);
    }
}
